/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NumberCustomEditor.java
 *
 * Created on 13-Jun-2011, 12:17:45
 */

package net.neilcsmith.praxis.live.pxr.editors;

import java.text.DecimalFormat;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.neilcsmith.praxis.core.Argument;
import net.neilcsmith.praxis.core.types.PNumber;
import org.openide.util.Exceptions;

/**
 *
 * @author Neil C Smith (http://neilcsmith.net)
 */
class NumberCustomEditor extends javax.swing.JPanel implements ChangeListener {

    private NumberEditor editor;
    private PNumber minimum;
    private PNumber maximum;
    private DecimalFormat labelFormat;


    public NumberCustomEditor(NumberEditor editor, PNumber minimum, PNumber maximum) {
        initComponents();
        this.editor = editor;
        this.minimum = minimum;
        this.maximum = maximum;
        initSlider();
        setLabelText(getCurrentValue());
        slider.addChangeListener(this);
    }


    private void initSlider() {
        double max = maximum.value();
        double min = minimum.value();
        if (maximum == minimum) {
            slider.setEnabled(false);
            return;
        }
        double ratio = (getCurrentValue() - min) / (max - min);
//        ratio = interpolator.reverseInterpolate(ratio);
        int mMin = slider.getMinimum();
        int mMax = slider.getMaximum();
        int val = (int) Math.round(ratio * (mMax - mMin));
        val += mMin;
        slider.setValue(val);
    }
    
    private void setLabelText(double value) {
        if (labelFormat == null) {
            labelFormat = new DecimalFormat("#.00");
        }
        numberField.setText(labelFormat.format(value));
    }

    private double getCurrentValue() {
        try {
            return PNumber.coerce((Argument) editor.getValue()).value();
        } catch (Exception ex) {
            return minimum.value();
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        slider = new javax.swing.JSlider();
        numberField = new javax.swing.JLabel();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(slider, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(numberField, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(numberField)
                .addContainerGap(236, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel numberField;
    private javax.swing.JSlider slider;
    // End of variables declaration//GEN-END:variables

    @Override
    public void stateChanged(ChangeEvent evt) {
        int min = slider.getMinimum();
        int max = slider.getMaximum();
        int x = slider.getValue();
        double ratio = ((double) (x - min)) / (max - min);
//        ratio = interpolator.interpolate(ratio);
        try {
            double value = (ratio * (maximum.value() - minimum.value()))
                    + minimum.value();
            editor.setValue(PNumber.valueOf(value));
            setLabelText(value);
        } catch (Exception e) {
            Exceptions.printStackTrace(e);
        }
    }


}
